<?php

namespace app\api\controller;

use app\api\model\AdminConfigs as AdminConfigsDB;
use app\api\model\Eggs as EggsDB;
use app\api\model\UserUseCards as UserUseCards;
use app\api\model\UserUseEggs as UserUseEggsDB;
use Illuminate\Support\Carbon;
use support\Request;
use app\api\model\InviteRecord as InviteRecordDB;
use app\api\model\User;
use app\api\model\Wallets as Wallets;
use app\api\model\AdminConfigs as AdminConfigs;



class InviteRecordController
{
    public function list(Request $request){
        $uid = $request->uid;
        $ids = InviteRecordDB::where('user_id',$uid)->pluck('invite_id');
        //判断标准1母鸡卡数2U蛋数3奖励数
        $u1 =AdminConfigs::where('id',2)->value('friend_buy_chicken_card_count');
        $u2 =AdminConfigs::where('id',2)->value('friend_produce_egg_count');
        $u3 =AdminConfigs::where('id',2)->value('reward_egg_count');
        //判断兑奖条件
        foreach ($ids as $k){
            $buy_cards_num =  Wallets::where('wallet_user_id',$k)->value('buy_cards_num');
            $used_eggs = UserUseCards::where('user_id', $k)->sum('done_eggs');
            if($buy_cards_num>=$u1&&$used_eggs>=$u2){
                InviteRecordDB::where('user_id',$uid)->where('is_exchange','=',2)->where('invite_id',$k)->update([
                    'get_eggs'=>$u3,
                    'is_exchange'=>0,
                    'updated_at'=>Carbon::now()->toDateTimeString()
                ]);
            }
        }
        $res['list'] = InviteRecordDB::where('user_id',$uid)
            ->join('user_logins', 'invite_record.invite_id', '=', 'user_logins.uid')
            ->select('invite_record.*','user_logins.nickname as invite_nickname')->get();
        $res['total'] = InviteRecordDB::where('user_id',$uid)->count();
        $res['total_eggs'] = InviteRecordDB::where('user_id',$uid)->where('is_exchange','1')->sum('get_eggs');
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }
    public  function inviteInfo(Request $request){
        $uid = $request->uid;
        $res['total_person'] = InviteRecordDB::where('user_id',$uid)->count();
        $res['total_eggs'] = InviteRecordDB::where('user_id',$uid)->where('is_exchange','1')->sum('get_eggs');
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }
    public function exchange(Request $request){
        $uid = $request->uid;
        $id = $request->post('id', '');
        $id_exchange = InviteRecordDB::where('id',$id)->value('is_exchange');
        if($id_exchange==2){
            return json([
                'code' => 0,
                'msg' => 'ok',
                'data' => '您未达标',
            ]);
        }
        if($id_exchange==1){
            return json([
                'code' => 0,
                'msg' => 'ok',
                'data' => '您已兑换',
            ]);
        }
        //更新邀请记录
        InviteRecordDB::where('id', $id)->update([
                'is_exchange'=>1,
                'updated_at'=>Carbon::now()->toDateTimeString()
            ]);
        //加U蛋
        $add_eggs = InviteRecordDB::where('id', $id)->value('get_eggs');
        $held_eggs = Wallets::where('wallet_user_id', $uid)->value('held_eggs');
        Wallets::where('wallet_user_id', $uid)->sharedLock()->update([
            'held_eggs' => $held_eggs+$add_eggs,
            'updated_at'=>Carbon::now()->toDateTimeString()
        ]);
        //添加U蛋记录
//        UserUseEggsDB::insertGetId([
//            'user_id'=>$uid,
//            'count'=>$add_eggs,
//            'use_type'=>'邀请奖励',
//            'type'=>1,
//            'created_at'=> Carbon::now()->toDateTimeString()
//        ]);
        //添加U蛋记录-自己的
        EggsDB::insertGetId([
            'uid' => $uid,
            'count' => $add_eggs,
            'type' => '邀请奖励',
            'yu_e' => $held_eggs+$add_eggs,
            'created_at' => Carbon::now()->toDateTimeString()
        ]);

        $friend_small = AdminConfigsDB::where('id', '2')->value('friend_small');
        if($friend_small ==1){
            $invite_id = InviteRecordDB::where('id', $id)->value('invite_id');
            $held_eggs1 = Wallets::where('wallet_user_id', $invite_id)->value('held_eggs');
            Wallets::where('wallet_user_id', $invite_id)->sharedLock()->update([
                'held_eggs' => $held_eggs1+$add_eggs,
                'updated_at'=>Carbon::now()->toDateTimeString()
            ]);
            //添加U蛋记录
//            UserUseEggsDB::insertGetId([
//                'user_id'=>$invite_id,
//                'count'=>$add_eggs,
//                'use_type'=>'邀请奖励',
//                'type'=>1,
//                'created_at'=> Carbon::now()->toDateTimeString()
//            ]);
            //添加U蛋记录-自己的
            EggsDB::insertGetId([
                'uid' => $invite_id,
                'count' => $add_eggs,
                'type' => '邀请奖励',
                'yu_e' => $held_eggs1+$add_eggs,
                'created_at' => Carbon::now()->toDateTimeString()
            ]);
        }




        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => '',
        ]);
    }

}
